package com.mmy.controller.car;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.car.TCarOrder;
import com.mmy.domain.car.TCarPlan;
import com.mmy.domain.car.VCarBase;
import com.mmy.domain.car.VCarOrder;
import com.mmy.domain.car.VCarPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.car.impl.CarOrderServiceImpl;
import com.mmy.service.car.impl.CarPlanServiceImpl;
import com.mmy.service.car.impl.TCarBaseServiceImpl;
import com.mmy.service.car.impl.TCarOrderServiceImpl;
import com.mmy.service.car.impl.TCarPlanServiceImpl;

/**
 * 类的介绍：车辆订单
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class CarOrderController {

	@Autowired
	TCarOrderServiceImpl tCarOrderServiceImpl;
	@Autowired
	TCarBaseServiceImpl tCarBaseServiceImpl;
	@Autowired
	TCarPlanServiceImpl tCarPlanServiceImpl;
	// Hanxl 整理 2020.10.29
	@Autowired
	CarOrderServiceImpl carOrderServiceImpl;
	// Hanxl 整理 2020.10.29
	@Autowired
	CarPlanServiceImpl carPlanServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 新建车辆预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
//获取车辆计划时间最大的对象
	public VCarPlan selectMaxOne(List<VCarPlan> lstCarPlan) {
		VCarPlan vCarPlan = new VCarPlan();

		Long dates[] = new Long[lstCarPlan.size()];
		for (int i = 1; i <= lstCarPlan.size(); i++) {
			// 把date类型的时间对象转换为long类型，时间越往后，long的值就越大，
			// 所以就依靠这个原理来判断距离现在最近的时间
			dates[i - 1] = lstCarPlan.get(i - 1).getEndDate().getTime();
		}

		Long maxIndex = dates[0];// 定义最大值为该数组的第一个数
		for (int j = 0; j < dates.length; j++) {
			if (maxIndex < dates[j]) {
				maxIndex = dates[j];

			}
			// 找到了这个j
			vCarPlan = lstCarPlan.get(j);
		}
		return vCarPlan;
	}

	// 获取车辆计划时间最小的对象
	public VCarPlan selectMinOne(List<VCarPlan> lstCarPlan) {
		VCarPlan vCarPlan = new VCarPlan();

		Long dates[] = new Long[lstCarPlan.size()];
		for (int i = 1; i <= lstCarPlan.size(); i++) {
			// 把date类型的时间对象转换为long类型
			// 所以就依靠这个原理来判断距离现在最远的时间
			dates[i - 1] = lstCarPlan.get(i - 1).getStartDate().getTime();

		}

		Long minIndex = dates[0];// 定义最小值为该数组的第一个数
		for (int j = 0; j < dates.length; j++) {
			if (minIndex > dates[j]) {
				minIndex = dates[j];

			}
			// 找到了这个j
			vCarPlan = lstCarPlan.get(j);
		}

		return vCarPlan;
	}
	
	@RequestMapping(value = "/enterreservecarbase")
	public String EnterReserveCarBase(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		List<VCarPlan> lstCarPlan = tCarPlanServiceImpl.findByCarID(iCarID);
		model.addAttribute("carplanlist", lstCarPlan);
		model.addAttribute("iCarID", iCarID);
		return "webbackground/car/reservecarbase";

	}

	/**
	 * 函数功能说明： 预留转订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterpayordercar")
	public String EnterPayOrderCar(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(iID);
		model.addAttribute("tCarOrder", tCarOrder);

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		model.addAttribute("iCarID", iCarID);

		return "webbackground/car/payordercar";

	}

	@RequestMapping(value = "/subpayordercar")
	public String PayOrderCar(TCarOrder tCarOrder, HttpSession session, String CarID) throws Exception {

		try {
//			 定义一个新的实体类对象 
			TCarOrder dtCarOrder = tCarOrderServiceImpl.findByID(tCarOrder.getID());

			dtCarOrder.setPayType(tCarOrder.getPayType());
			dtCarOrder.setOrderState((byte) 2);
			dtCarOrder.setOrderDate(new Date());
			dtCarOrder.setOrderName(tCarOrder.getOrderName());
			dtCarOrder.setOrderTel(tCarOrder.getOrderTel());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtCarOrder.setOrderUserID(tSysUser.getUserID());
			dtCarOrder.setReceivable(tCarOrder.getReceivable());
			dtCarOrder.setVoucher(tCarOrder.getVoucher());
			dtCarOrder.setReceipt(tCarOrder.getReceipt());
			tCarOrderServiceImpl.save(dtCarOrder);

			return "redirect:printcar?iID=" + tCarOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 取消订单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelordercar")
	public String EnterCancelOrderCar(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(iID);
		model.addAttribute("tCarOrder", tCarOrder);

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		model.addAttribute("iCarID", iCarID);

		return "webbackground/car/cancelordercar";

	}

	@RequestMapping(value = "/subcancelordercar")
	public String CancelOrderCar(TCarOrder tCarOrder, HttpSession session, String CarID) throws Exception {

			TCarOrder dtCarOrder = tCarOrderServiceImpl.findByID(tCarOrder.getID());
			dtCarOrder.setOrderState((byte) 4);
			dtCarOrder.setRefund(tCarOrder.getRefund());
			tCarOrderServiceImpl.save(dtCarOrder);
			return "redirect:printcancelscenic?iID=" + tCarOrder.getID();
	}

	/**
	 * 函数功能说明： 修改车辆预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdatereservecar")
	public String EnterUpdateReserveCar(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(iID);
		model.addAttribute("tCarOrder", tCarOrder);
		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		model.addAttribute("iCarID", iCarID);
		return "webbackground/car/updatereservecar";

	}

	@RequestMapping(value = "/updatereservecar")
	public String UpdateReserveCarBase(TCarOrder tCarOrder, HttpSession session, String CarID) throws Exception {

			TCarOrder dtCarOrder = tCarOrderServiceImpl.findByID(tCarOrder.getID());
			dtCarOrder.setReserveName(tCarOrder.getReserveName());
			dtCarOrder.setReserveTel(tCarOrder.getReserveTel());
			dtCarOrder.setMemo(tCarOrder.getMemo());
			dtCarOrder.setModifyDate(new Date());
			BigDecimal bPrice = dtCarOrder.getPrice();
			dtCarOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtCarOrder.setReviser(tSysUser.getUserName());
			tCarOrderServiceImpl.save(dtCarOrder);
			return "redirect:scenicbaseinfo?iScenicID=" + Integer.parseInt(CarID);
	}

	/**
	 * 函数功能说明： 取消预留
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelreservecar")
	public String CancelReserveCar(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(iID);
		tCarOrder.setOrderState((byte) 3);
		tCarOrderServiceImpl.save(tCarOrder);
		model.addAttribute("tCarOrder", tCarOrder);
		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		model.addAttribute("iCarID", iCarID);
		VCarBase vCarBase = tCarBaseServiceImpl.findByID(iCarID);
		model.addAttribute("vCarBase", vCarBase);
		List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCarID(iCarID);
		model.addAttribute("orderlist", lstCarOrder);
		return "webbackground/car/carbaseinfo";

	}

	/**
	 * 函数功能说明： 修改车辆订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateordercar")
	public String EnterUpdateOrderCar(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TCarOrder tCarOrder = tCarOrderServiceImpl.findByID(iID);
		model.addAttribute("tCarOrder", tCarOrder);
		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		model.addAttribute("iCarID", iCarID);
		return "webbackground/car/updateordercar";
	}

	@RequestMapping(value = "/updateordercar")
	public String UpdateOrderCarBase(TCarOrder tCarOrder, HttpSession session, String CarID) throws Exception {

			TCarOrder dtCarOrder = tCarOrderServiceImpl.findByID(tCarOrder.getID());
			dtCarOrder.setOrderName(tCarOrder.getOrderName());
			dtCarOrder.setOrderTel(tCarOrder.getOrderTel());
			dtCarOrder.setMemo(tCarOrder.getMemo());
			dtCarOrder.setModifyDate(new Date());
			BigDecimal bPrice = dtCarOrder.getPrice();
			dtCarOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtCarOrder.setReviser(tSysUser.getUserName());
			tCarOrderServiceImpl.save(dtCarOrder);
			return "redirect:carbaseinfo?Car=" + Integer.parseInt(CarID);
	}

	/**
	 * 函数功能说明： 删除取消预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delreservecarbase")
	public String DelReserveCarBase(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();
		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}
		tCarOrderServiceImpl.delCarOrder(lstID);
		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		model.addAttribute("iCarID", iCarID);
		VCarBase vCarBase = tCarBaseServiceImpl.findByID(iCarID);
		model.addAttribute("vCarBase", vCarBase);
		List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCarID(iCarID);
		model.addAttribute("orderlist", lstCarOrder);
		return "webbackground/car/carbaseinfo";
	}

	/**
	 * 函数功能说明： 删除取消订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delordercarbase")
	public String DelOrderCarBase(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tCarOrderServiceImpl.delCarOrder(lstID);

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		model.addAttribute("iCarID", iCarID);

		VCarBase vCarBase = tCarBaseServiceImpl.findByID(iCarID);
		model.addAttribute("vCarBase", vCarBase);

		List<VCarOrder> lstCarOrder = tCarOrderServiceImpl.findByCarID(iCarID);
		model.addAttribute("orderlist", lstCarOrder);

		return "webbackground/car/carbaseinfo";
	}

	/**
	 * 函数功能说明： 前台个人中心点击取消车辆预留
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entercarcancelreserve")
	public String enterCarCancelReserve(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		carOrderServiceImpl.updateState(iID, sessionSysUser);
		return "redirect:mycarreserve";
	}

	/**
	 * 函数功能说明： 前台个人中心点击申请车辆退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entercarapplyrefund")
	public String enterCarApplyRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		carOrderServiceImpl.updateRefundState(iID, sessionSysUser);
		return "redirect:mycarorder";
	}

	/**
	 * 函数功能说明： 前台个人中心点击车辆撤销退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entercarcancelrefund")
	public String enterCarCancelRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		carOrderServiceImpl.updateCancelState(iID, sessionSysUser);
		return "redirect:mycarrefund";
	}

	/**
	 * 函数功能说明： 前台个人中心点击显示详情
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/mycarinfo")
	public String toMyCarInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TCarOrder dbCarOrder = carOrderServiceImpl.findByID(iID);
		model.addAttribute("dbCarOrder", dbCarOrder);
		String strOrderID = dbCarOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(6, 12));
		VCarPlan dbCarPlan = carPlanServiceImpl.findByID(iPlanID);
		model.addAttribute("dbCarPlan", dbCarPlan);
		return "permanagement/MyCarInfo";
	}
}
